package com.green_reading.mandolin;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class DBManager {
    private SQLiteDBHelper helper;
    private SQLiteDatabase db;

    public DBManager(Context context) {
        helper = new SQLiteDBHelper(context);
        db = helper.getWritableDatabase();
    }

    public Long addMessage(Integer from, Integer to, String content, Integer read, Long create_time) {
        Long update_time = Util.getDatetimeString();
        ContentValues cv = new ContentValues();
        cv.put("message_id", 0);
        cv.put("from_", from);
        cv.put("to_", to);
        cv.put("content", content);
        cv.put("read", read);
        cv.put("deleted", 0);
        cv.put("create_time", create_time);
        cv.put("update_time", update_time);
        db.beginTransaction();
        try {
            Long id = db.insert(SQLiteDBHelper.TABLE_NAME, null, cv);
            db.setTransactionSuccessful();
            return id;
        } finally {
            db.endTransaction();
        }
    }

    public void updateMessageId(JSONObject message_ids) {
        Iterator<String> keys = message_ids.keys();
        while(keys.hasNext()) {
            String key = keys.next();
            ContentValues cv = new ContentValues();
            cv.put("message_id", message_ids.optString(key));
            cv.put("read", 1);
            db.update(SQLiteDBHelper.TABLE_NAME, cv, "id = ?", new String[]{key});
        }
    }

    public void deleteMessageById(Integer id) {
        db.delete(SQLiteDBHelper.TABLE_NAME, "id = ?", new String[]{String.valueOf(id)});
    }

    public void deleteMessage(Integer from, Integer to) {
        db.delete(SQLiteDBHelper.TABLE_NAME, "from_ = ? or to_ = ?",
                new String[]{String.valueOf(from), String.valueOf(to)});
    }

    public List<Map<String, String>> queryMessage(Integer from, Integer to) {
        ArrayList<Map<String, String>> messages = new ArrayList<Map<String, String>>();
        Cursor cursor = db.rawQuery("select * from " + SQLiteDBHelper.TABLE_NAME + " where " +
                "from_ = ? or to_ = ? order by create_time asc",
                new String[] {String.valueOf(from), String.valueOf(to)});
        while (cursor.moveToNext()) {
            HashMap<String, String> content = new HashMap<String, String>();
            content.put("content", cursor.getString(cursor.getColumnIndex("content")));
            content.put("create_time", cursor.getString(cursor.getColumnIndex("create_time")));
            messages.add(content);
        }
        cursor.close();
        return messages;
    }

    public List<Map<String, String>> queryUnSendMessage() {
        ArrayList<Map<String, String>> messages = new ArrayList<Map<String, String>>();
        Cursor cursor = db.rawQuery("select * from " + SQLiteDBHelper.TABLE_NAME + " where " +
                "read = 0 and message_id = 0", null);
        while (cursor.moveToNext()) {
            HashMap<String, String> content = new HashMap<String, String>();
            content.put("id", String.valueOf(cursor.getInt(cursor.getColumnIndex("id"))));
            content.put("message_id",
                    String.valueOf(cursor.getInt(cursor.getColumnIndex("message_id"))));
            content.put(
                    "from", String.valueOf(cursor.getInt(cursor.getColumnIndex("from_"))));
            content.put(
                    "to", String.valueOf(cursor.getInt(cursor.getColumnIndex("to_"))));
            content.put("content", cursor.getString(cursor.getColumnIndex("content")));
            content.put("create_time", cursor.getString(cursor.getColumnIndex("create_time")));
            messages.add(content);
        }
        cursor.close();
        return messages;
    }


    public void close() {
        db.close();
    }
}
